﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;

public partial class capcha : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        fncDrawCaptcha();
    }
    public void fncDrawCaptcha()
    {

        //*** Editable Values
        int[] BackgroundColor = new int[] { 255, 255, 255 };// The 3 numbers represent in order RED, GREEN, BLUE for the captcha's background color
        bool RandomBackgroundNoiseColor = false;// True / False. If you choose True, BackgroundNoiseColor will not apply 
        bool RandomTextColor = true;// True / False. If you choose True, TextColor will not apply 
        int[] BackgroundNoiseColor = new int[] { 150, 150, 150 };// The 3 numbers represent in order RED, GREEN, BLUE
        int[] TextColor = new int[] { 200, 200, 200 };// The 3 numbers represent in order RED, GREEN, BLUE
        HatchStyle BackgroundNoiseTexture = HatchStyle.Min;// replace ".Min" with any of the following: Horizontal, Vertical, ForwardDiagonal, BackwardDiagonal, Cross, DiagonalCross, Percent05, Percent10, Percent20, Percent25, Percent30, Percent40, Percent50, Percent60, Percent70, Percent75, Percent80, Percent90, LightDownwardDiagonal, LightUpwardDiagonal, DarkDownwardDiagonal, DarkUpwardDiagonal, WideDownwardDiagonal, WideUpwardDiagonal, LightVertical, LightHorizontal, NarrowVertical, NarrowHorizontal, DarkVertical, DarkHorizontal, DashedDownwardDiagonal, DashedUpwardDiagonal, DashedHorizontal, DashedVertical, SmallConfetti, LargeConfetti, ZigZag, Wave, DiagonalBrick, HorizontalBrick, Weave, Plaid, Divot, DottedGrid, DottedDiamond, Shingle, Trellis, Sphere, SmallGrid, SmallCheckerBoard, LargeCheckerBoard, OutlinedDiamond, SolidDiamond, LargeGrid, Min, Max
        int length = 6;// Number of characters to generate
        //*** END Editable Values

        int height = 50;
        int width = 100;
        width = width + ((length - 6) * 30);
        Random ranRotate = new Random();

        string strText = System.Guid.NewGuid().ToString();
        strText = strText.Replace("-", String.Empty);
        strText = strText.Substring(0, length);

        Bitmap bmpCanvas = new Bitmap(width, height, PixelFormat.Format24bppRgb);
        Graphics graCanvas = Graphics.FromImage(bmpCanvas);
        RectangleF recF = new RectangleF(0, 0, width, height);
        Brush bruBackground = default(Brush);
        SolidBrush letterBrush = default(SolidBrush);

        graCanvas.TextRenderingHint = TextRenderingHint.AntiAlias;

        if (RandomBackgroundNoiseColor == true)
        {
            bruBackground = new HatchBrush(BackgroundNoiseTexture, Color.FromArgb((ranRotate.Next(0, 255)), (ranRotate.Next(0, 255)), (ranRotate.Next(0, 255))), Color.FromArgb(BackgroundColor[0], BackgroundColor[1], BackgroundColor[2]));
        }
        else
        {
            bruBackground = new HatchBrush(BackgroundNoiseTexture, Color.FromArgb(BackgroundNoiseColor[0], BackgroundNoiseColor[1], BackgroundNoiseColor[2]), Color.FromArgb(BackgroundColor[0], BackgroundColor[1], BackgroundColor[2]));
        }

        graCanvas.FillRectangle(bruBackground, recF);

        if (RandomTextColor == true)
        {
            letterBrush = new SolidBrush(Color.Black);
        }
        else
        {
            letterBrush = new SolidBrush(Color.Black);
        }

        System.Drawing.Drawing2D.Matrix matRotate = new System.Drawing.Drawing2D.Matrix();
        int i = 0;
        for (i = 0; i <= strText.Length - 1; i++)
        {
            matRotate.Reset();
            int intChars = strText.Length;
            int x = width / (intChars + 1) * i;
            int y = height / 2;
            //matRotate.RotateAt(ranRotate.Next(-30, 30), new PointF(width / (intChars + 1) * i, height * 0.5) );
            matRotate.RotateAt(ranRotate.Next(-30, 30), new PointF(x, y));
            graCanvas.Transform = matRotate;
            if (i == 0)
            {
                //draw ‘the text on our image
                //graCanvas.DrawString(strText.Chars(i), new Font("Comic Sans MS", 25, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, height * 0.4);
                graCanvas.DrawString(strText.Substring(i, 1), new Font("Comic Sans MS", 16, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 20);
            }
            else if (i == 1)
            {
                //draw ‘the text on our image
                graCanvas.DrawString(strText.Substring(i, 1), new Font("Arial", 16, FontStyle.Bold), letterBrush, width / (intChars + 1) * i, 10);
            }
            else if (i == 2)
            {
                //draw ‘the text on our image
                graCanvas.DrawString(strText.Substring(i, 1), new Font("Times New Roman", 16, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 20);
            }
            else if (i == 3)
            {
                //draw ‘the text on our image
                graCanvas.DrawString(strText.Substring(i, 1), new Font("Georgia", 16, FontStyle.Bold), letterBrush, width / (intChars + 1) * i, 10);
            }
            else if (i == 4)
            {
                //draw ‘the text on our image
                graCanvas.DrawString(strText.Substring(i, 1), new Font("Verdana", 16, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 20);
            }
            else if (i == 5)
            {
                //draw ‘the text on our image
                graCanvas.DrawString(strText.Substring(i, 1), new Font("Geneva", 16, FontStyle.Bold), letterBrush, width / (intChars + 1) * i, 10);
            }
            else
            {
                //draw ‘the text on our image
                graCanvas.DrawString(strText.Substring(i, 1), new Font("Verdana", 16, FontStyle.Italic), letterBrush, width / (intChars + 1) * i, 20);
            }
            graCanvas.ResetTransform();
        }

        //bmpCanvas.Save(path, ImageFormat.Gif);

        System.IO.MemoryStream ms = new System.IO.MemoryStream();
        bmpCanvas.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
        Response.ClearContent();
        Response.ContentType = "image/Jpeg";
        Response.BinaryWrite(ms.ToArray());

        graCanvas.Dispose();
        bmpCanvas.Dispose();

        Session["captcha"] = strText;
    }

}